<?php
/*function __autoload($dir)
{
		switch (true)
		{
			case is_file(BASE_PATH."/classes/".$dir.".class.php") :
				include(BASE_PATH."/classes/".$dir.".class.php");
			break;
			case is_file(BASE_PATH."/classes/static/".$dir.".class.php") :
				include(BASE_PATH."/classes/static/".$dir.".class.php");
			break;
			case is_file(BASE_PATH."/classes/".$dir.".inc.php") :
				include(BASE_PATH."/classes/".$dir.".inc.php");
			break;
			case is_file(BASE_PATH."/classes/".$dir.".inc") :
				include(BASE_PATH."/classes/".$dir.".inc");
			break;
		}
}
/**/
/**
 * @author tuadmin
 * @version 1.3.13
 * +added & scope function(string '') and extract($escope,EXTR_REFS); is powerfull
 * --version 1.3.12--
 * +fix explode function
 * --version 1.3.11--
 * +added explode function
 * +added txtToId function
 * +added getNotEmpty function (&var_reference,$option_alternative);
 * +added & getNotEmpty function (&var_reference,$option_alternative);
 * +added & get function (&$var,$alternative_value);
 */
class system
{
	public static $import = null;
	public static $temp = '';
	public static function blowfish_encode($string, $key)
	{
		if(empty($key)){ return false;}
		else
		{
			$data = utf8_decode($string);
		}
		$pma_cipher = new myBlowfish();
		$encrypt = '';
		$o = strlen($data);
		for ($i=0;  $i< $o ; $i+=8)
		{
			$block = substr($data, $i, 8);
			if (strlen($block) < 8)
			{
				$block = full_str_pad($block, 8, "\0", 1);
			}
			$encrypt .= $pma_cipher->encryptBlock($block, $key);
		}
		unset($pma_cipher);
		return base64_encode($encrypt);
	}
	public static function blowfish_decode($string, $key)
	{
			if(empty($key)){ return false;}
			$pma_cipher = new myBlowfish();
			$decrypt = '';
			$data = base64_decode($string);
			$o = strlen($data);
			for ($i=0; $i< $o; $i+=8) {
				$decrypt .= $pma_cipher->decryptBlock(substr($data, $i, 8), $key);
			}
			unset($pma_cipher);
			return trim($decrypt);
	}
	public function myEnCrypt($data,$key)
	{

		$count = strlen($key);
		$c = $data;
		return base64_encode($data);
	}
	public function myDeCrypt($data,$key)
	{
		$count = strlen($key);
		return base64_decode($data);
	}
	public static function main()
	{
		self::$import = new system();
	}
	public static function import( $class)
	{
		is_object($class) || die("system::import(new example()); is correct");
		$name =  str_replace("::","->",get_class($class));

		eval('self::$import->'.$name.' = $class; ');
		return $class;
	}
	public function __get($name)
	{
		//self::importing($name);
		$this->$name = (object) null;
		return  $this->$name;
	}

	/**
	 * @param ByReference $var
	 * @param String $alternative
	 * @param boolean $htmlentities_quot
	 * @return Ambigous <boolean, string>
	 */
	static public function & is_set( & $var,$alternative = false,$htmlentities_quot = false)
	{
		return self::getIsSet($var,$alternative,$htmlentities_quot);
	}
	static public function get( & $var,$alternative = false,$htmlentities_quot = false)
	{
		return (isset($var))? $var :$alternative;
	}
	/**
	 * Return $var if defined or use boolean false if not defined or use value in $alternative
	 * @param ByRegerence $var
	 * @param String $alternative
	 * @param boolean $htmlentities_quot
	 * @return Ambigous <boolean, string>
	 */
	static public function & getIsSet( & $var,$alternative = false,$htmlentities_quot = false)
	{
		$return = (isset($var))? $var :$alternative;
		if($htmlentities_quot !== false)
		{
			$return = strtr($return ,array('"'=>'&quot;'));
		}
		return $return;
	}
	/**
	 * Return $var if defined or use boolean false if not defined or use value in $alternative
	 * @param & $var
	 * @param String $alternative if not exist $var use $alternative
	 * @param boolean $htmlentities_quot
	 * @return <boolean, string>
	 */
	static public function exist(& $var,$alternative = false,$htmlentities_quot = false)
	{
		$return = (isset($var))? $var :$alternative;
		if($htmlentities_quot !== false)
		{
			$return = strtr($return ,array('"'=>'&quot;'));
		}
		return $return;
	}
		/**
	 * Return $var if defined or use boolean false if not defined or use value in $alternative
	 * @param & $var
	 * @param String $alternative if not exist $var use $alternative
	 * @param boolean $htmlentities_quot
	 * @return <boolean, string>
	 */
	static public function & getNotEmpty(& $var,$alternative = false,$htmlentities_quot = false)
	{
		if(empty($var))
		{
			return $alternative ;
		}
		return $var;
		//$var_Ref = empty($var)? $alternative : $var;
		//return $var_Ref;
	}
	static public function redirect($url,$seconds = 0)
	{
		$url_php = strtr($url,array("&amp;" => "&"));
		if($seconds < 1 && !headers_sent())
		{
			header ("Location: $url_php");
		}
			$secondsJS = $seconds * 1000;
		$JS = <<<JS
			var time = null;
			function move(){
				location.href="$url";
				window.location="$url";
			}
			setTimeout("move()","$secondsJS")
JS;
		if(class_exists('mySkin'))
		{
			mySkin::addJs($JS);
		}
		else
		{
			die ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<title>redireccionando</title>
			<meta http-equiv="Refresh" content="'.$seconds.';url=' . $url . '">
			<script type="text/javascript">
	//<![CDATA[
	$JS
	//]]>
			</script>
		</head>
		<body onload="timer=setTimeout(\'move()\','.$secondsJS.')">
			<a href="' . $url . '">redirect</a>
		</body>
	</html>');
		}
	}
	static public function arrayDeleteEmptys(& $array)
	{
		if(is_array($array))
		{
			foreach($array as $key => $value)
			{
				if(trim($value) =='')
				{
					unset ($array[$key]);
				}
			}
		}

	}
	static public function arrayFilter($array,$valid_keys = array(),$list_black = false)
	{
		$new = array();
		foreach($valid_keys as $key)
		{
			if($list_black == false && isset($array[$key]))
			{
				$new[$key] = $array[$key];
			}
			else if($list_black && isset($array[$key]))
			{
				unset($array[$key]);
				continue;
			}
			
		}
		return $list_black==true ?$array : $new ;
	}
	static public function randomAlphaNumber($length = 0)
	{
		$table = md5(rand(1,99999));
		$ini_len = strlen($table)-$length;
		return substr($table,mt_rand(0,$ini_len),$length);
	}
	/**
	* function scope('example','exaple2') return array('example' => null,'example2' => null); and  extract($scope,EXTR_REFS); is powerfull
	*/
	static public function & scope($array)
	{
		$return = array();
		if(is_string($array))
		{
			$array = func_get_args();
		}
		$return = array_fill_keys($array,null);
		return $return;
	}
	static public function explode($separator,$string,$char_escape = '\\',$limit = null)
	{
		$new = array();/*
		if(is_string($string))
		{
			$temp = new arrayIterator(explode($separator,$string));
			$i = 0;
			foreach($temp as $key => $n)
			{
				if(substr($n,-1,1) == $char_escape)
				{
					$new[$i] = substr($n,0,-1).$separator . $temp[$key+1];
					$temp->next();
				}
				else
				{
					$new[$i] = $n;
				}
				$i++;
			}unset($temp);
		}
		return $new;*/
		if(is_string($string))
		{
			//return preg_split('#(?<!'.$char_escape.'\\)'.$char_escape. $separator.'#',$string,$limit);
			foreach( preg_split('#(?<!'.$char_escape.')\\'.$separator.'#',$string,$limit) as $value)
			{
				$new[] = strtr($value,array($char_escape.$separator=>$separator));
			}			
		}
		return $new;
	}
	/**
	 * convert text to id input:"hola niño" output:"hola ninho"
	 */
	public static function txtToId($id)
	{
		//kita espacios en blanco en exceso
		$id = preg_replace('/\s\s+/', ' ', $id);
		//conversion Ñ a letra portuguesa
		$id = strtr(utf8_decode($id), array(utf8_decode("ñ")=>'nh',utf8_decode("Ñ") => 'NH'));
		$no = utf8_decode('áéíóúÁÉÍÓÚñÑçÇ ');
		$si =             'aeiouAEIOUnNcC-';
		//$resultado = strtolower(strtr(utf8_decode($id), $no,$si));
		$resultado = str_replace(str_split($no),str_split($si),$id);
		//quitamos caracteres no validos
		return preg_replace('/([^a-zA-Z0-9\-\.\:,;!¡\+\_\*\=\%\$@\(\)\{\}\[\]]+)/', '', $resultado);
	}
}
